package com.atguigu.nio;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NioFileChannel01 {
    public static void main(String[] args) throws IOException {
        String str="hello 尚硅谷";
        //创建一个输出流
        FileOutputStream fileOutputStream = new FileOutputStream("e:\\file01.txt");
        //通过输出流获取文件channel
        //channel的真实类型是FileChannelImpl
        FileChannel channel = fileOutputStream.getChannel();
        //创建一个缓冲区ByteBuffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //将数据写入到buffer
        buffer.put(str.getBytes());
        //读写切换position重新置位0,limit变成15
        buffer.flip();
        //将buffer里的数据写入到channel
        channel.write(buffer);
        fileOutputStream.close();
    }
}
